package com.dp.appi.dao.impl;

import com.dp.appi.dao.ExpPhotoDao;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoji on 2016/5/12.
 */
@Component
public class ExpPhotoDaoImpl extends BaseDaoImpl implements ExpPhotoDao {
    /**
     * 查询踏勘照片
     *
     * @param exploration_ids 踏勘id
     * @return
     */
    @Override
    public Map<String, Object> expPhotoList(Long exploration_ids) {
        //一类照片
        String sql1="select photo_id,photo_type,photo_url,photo_thumb,photo_url from p_exploration_photo where photo_type=? and exploration_id=? ";
        List<Map<String,Object>> list1=getJdbcTemplate().queryForList(sql1,new Object[]{1,exploration_ids});
        //二类照片
        List<Map<String,Object>> list2=getJdbcTemplate().queryForList(sql1,new Object[]{2,exploration_ids});
        //三类照片
        List<Map<String,Object>> list3=getJdbcTemplate().queryForList(sql1,new Object[]{3,exploration_ids});
        Map<String,Object> result=new HashMap<>();
        Map<String,Object> result1=new HashMap<>();
        result1.put("photoNum",list1.size());
        result1.put("photoList",list1);
        Map<String,Object> result2=new HashMap<>();
        result2.put("photoNum",list2.size());
        result2.put("photoList",list2);
        Map<String,Object> result3=new HashMap<>();
        result3.put("photoNum",list3.size());
        result3.put("photoList",list3);
        result.put("onePhoto",result1);
        result.put("twoPhoto",result2);
        result.put("threePhoto",result3);
        return result;
    }

    /**
     * 保存踏勘照片
     *
     * @param imgUrl         照片路径
     * @param photo_thumb   缩略图
     * @param exploration_id 踏勘id
     * @param photo_type     照片类型 一类照片 1、二类照片 2、三类照片 3
     * @return
     */
    @Override
    public Long saveExpPhoto(final String imgUrl,final Long exploration_id,final Integer photo_type,final String photo_thumb) {
        final String sql="insert into p_exploration_photo(exploration_id,photo_type,photo_thumb,photo_url,create_time) values(?,?,?,?,NOW())";
        KeyHolder key = new GeneratedKeyHolder();
        getJdbcTemplate().update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                ps.setLong(1, exploration_id);
                ps.setInt(2, photo_type);
                ps.setString(3, photo_thumb);
                ps.setString(4, imgUrl);
                return ps;
            }
        }, key);
        long id2 = key.getKey().longValue();
        return id2;
    }

    /**
     * 删除踏勘照片
     *
     * @param photo_id 照片id
     */
    @Override
    public void delExpPhoto(Long photo_id) {
        String sql="delete from p_exploration_photo where photo_id=?";
        getJdbcTemplate().update(sql,new Object[]{photo_id});
    }
}
